package jmcnet.libcommun.containers.carousel
{
import flash.display.*;
import flash.events.Event;
import flash.filters.DisplacementMapFilter;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.net.URLRequest;

import mx.controls.Label;
import mx.states.AddChild;

public class CarouselItem extends Sprite
{
public var _angle:Number;
public var distance:Number;
private var pictLoad:Bitmap;
private var pictLdr:Loader;
public var vaguesBitmap:BitmapData;
public var vagues:Bitmap = new Bitmap();
public var offset:Point = new Point(0,0);
public var ImageBitmap:BitmapData;
public var ReflectBitmap:BitmapData;
public var Image:Bitmap;
public var Reflect:Bitmap;
public var displacFilter:DisplacementMapFilter = new DisplacementMapFilter();
public var isWave:Boolean = false;
private var _alphaGradientBitmap: BitmapData;
public var _falloff: Number = 0.6;
public var HSize:Number;
public var WSize:Number;			

public function CarouselItem(logo:String){
	loadImage(logo);
}

public function loadImage(logo:String):void {
	pictLdr = new  Loader();
	pictLdr.contentLoaderInfo.addEventListener(Event.COMPLETE, onImgLoaded);
	/* var pictURL:String = "http://flex.phiphou.com/carrousel/assets/"+logo; */
	var pictURL:String = logo;
	var pictURLReq:URLRequest = new URLRequest(pictURL);
	pictLdr.load(pictURLReq);
}

public function onImgLoaded(event:Event):void {
	pictLoad = Bitmap(pictLdr.content);
	HSize = pictLoad.height;
	WSize = pictLoad.width;
	ReflectBitmap = new BitmapData(WSize, HSize, true, 0x00000000)
	vaguesBitmap =new BitmapData(WSize, HSize, true, 0x00000000);
	Image = new Bitmap(pictLoad.bitmapData);
	
	addChild(Image);       
	createReflect();

	var rect: Rectangle = new Rectangle(0, 0,WSize , HSize);
				
	ReflectBitmap.fillRect(rect, 0xffffffff);
	ReflectBitmap.copyPixels(Image.bitmapData, rect, new Point(0,0), _alphaGradientBitmap);
	Reflect = new Bitmap(ImageBitmap);
	Reflect.bitmapData = ReflectBitmap;
	var transform: Matrix = new Matrix();
	transform.scale(1, -1);
	transform.translate(0, HSize*2+distance);
	Reflect.transform.matrix = transform;
				
	vagues.bitmapData = vaguesBitmap;
	vagues.transform.matrix = transform;
				
	displacFilter.mapBitmap = vaguesBitmap;
	displacFilter.mode = "clamp";
	displacFilter.scaleX = 4;
	displacFilter.scaleY = 10;
	displacFilter.componentX = 1;
	displacFilter.componentY = 1;
	displacFilter.alpha = 1;
					
	addChild(Reflect);
}

public function Wave():void
{
	if(vaguesBitmap != null && isWave){		
		Reflect.filters = [displacFilter];		
		offset.x += 2;
		offset.y += 2;
		vaguesBitmap.perlinNoise(200, 200, 2, Math.random(), true, false, 8, true, [offset]);
	}
}
       		
private function createReflect(): void {
	
	if (_alphaGradientBitmap == null) {
		
		_alphaGradientBitmap = new BitmapData(WSize, HSize, true, 0x00000000);
			
		var gradientMatrix: Matrix = new Matrix();
		var gradientSprite: Sprite = new Sprite();
			
		gradientMatrix.createGradientBox(WSize, HSize * _falloff, Math.PI/2, 0, HSize * (1.0 - _falloff));	
		gradientSprite.graphics.beginGradientFill(GradientType.LINEAR, [0x000000, 0x000000], [0, 1], [0, 255], gradientMatrix);			
		gradientSprite.graphics.drawRect(0, HSize * (1.0 - _falloff), WSize, (HSize*2) * _falloff);			
		gradientSprite.graphics.endFill();
			
		_alphaGradientBitmap.draw(gradientSprite, new Matrix());
			
		}
			
	}


}
}